/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.ConexionBD;
import Acceso.Security;
import java.sql.*;

/**
 *
 * @author Rafael
 */
public class listaResumenEmpleados extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador","Director DSI","Jefe Apoyo Administrativo","Personal Administrativo"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        System.out.println("Paso Autenticación");

        String voe = request.getParameter("voe");
        String ci = request.getParameter("ci");
        String usbid = request.getParameter("usbid");
        String nombre1 = request.getParameter("nombre1");
        String nombre2 = request.getParameter("nombre2");
        String apellido1 = request.getParameter("apellido1");
        String apellido2 = request.getParameter("apellido2");
        String CIcompuesta = voe + "-" + ci;

        response.setContentType("text/json");
        JSONObject data = new JSONObject();

        data.put("PersonalResumen", consultarResumenEmpleados(CIcompuesta, usbid, nombre1, nombre2, apellido1, apellido2));
        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }

    public JSONObject consultarResumenEmpleados(String CI, String usbid,
            String nombre1, String nombre2, String apellido1, String apellido2) {
        ConexionBD conex = new ConexionBD();
        JSONObject tiposObjeto = new JSONObject();

        try {
            Statement st = (Statement) conex.session.createStatement();


            String query = "SELECT * FROM comunidad_universitaria, personal_usb WHERE";

            if (CI != null && CI.length() >= 1) {
                query = query + " (comunidad_universitaria.ci = '" + CI + "'";
            }
            if (nombre1 != null && nombre1.length() >= 1) {
                query = query + " OR comunidad_universitaria.nombre_1 = '" + nombre1 + "'";
            }
            if (nombre2 != null && nombre2.length() >= 1) {
                query = query + " OR comunidad_universitaria.nombre_2 = '" + nombre2 + "'";
            }
            if (apellido1 != null && apellido1.length() >= 1) {
                query = query + " OR comunidad_universitaria.apellido_1 = '" + apellido1 + "'";
            }
            if (apellido2 != null && apellido2.length() >= 1) {
                query = query + " OR comunidad_universitaria.apellido_2 = '" + apellido2 + "'";
            }
            if (usbid != null && usbid.length() >= 1) {
                query = query + " OR personal_usb.usbid = '" + usbid + "'";
            }

            query = query + ") AND comunidad_universitaria.ci = personal_usb.ci"
                    + " AND personal_usb.activo = 'si';";


            System.out.println("\n\n\n\n\nAQUI " + query);

            ResultSet rs = st.executeQuery(query);
            int i = 0;
            while (rs.next()) {
                JSONObject tipoObjeto = new JSONObject();
                tipoObjeto.put("ci", rs.getString(1));
                tipoObjeto.put("nombre_1", rs.getString(2));
                tipoObjeto.put("nombre_2", rs.getString(3));
                tipoObjeto.put("apellido_1", rs.getString(4));
                tipoObjeto.put("apellido_2", rs.getString(5));
                tipoObjeto.put("usbid", rs.getString(10));
                tiposObjeto.put("" + i++, tipoObjeto);
                System.out.println("\n\n\n\n\n AQUI ENTRE " + i);
            }
        } catch (Exception e) {
        }
        return tiposObjeto;
    }
}
